package br.com.padtec.software.snmp.challenge;

import com.adventnet.snmp.mibs.MibNode;
import com.adventnet.snmp.mibs.MibOperations;
import com.adventnet.snmp.snmp2.SnmpAPI;
import com.adventnet.snmp.snmp2.SnmpException;
import com.adventnet.snmp.snmp2.SnmpOID;
import com.adventnet.snmp.snmp2.SnmpPDU;
import com.adventnet.snmp.snmp2.SnmpVar;
import com.adventnet.snmp.snmp2.SnmpVarBind;
import com.adventnet.snmp.snmp2.UDPProtocolOptions;

public class SnmpCommandSender {

  public SnmpPDU sendSet(MibOperations mibOps, SnmpServer snmpServer, int attempts, UDPProtocolOptions remoteHost,
    String community, int packetTimeout, SnmpVarBind snmpVarBind) throws SnmpServerException {
    SnmpPDU pdu = createSetPdu(remoteHost, community, packetTimeout);
    pdu.addVariableBinding(snmpVarBind);
    SnmpPDU resp = SnmpUtils.syncSend(snmpServer, pdu, 0);
    return resp;
  }

  public SnmpPDU createSetPdu(UDPProtocolOptions remoteHost, String writeCommunity, int packetTimeout) {
    SnmpPDU pdu = new SnmpPDU();
    pdu.setCommand(SnmpAPI.SET_REQ_MSG);
    pdu.setProtocolOptions(remoteHost);
    pdu.setWriteCommunity(writeCommunity.equals("") ? null : writeCommunity);
    pdu.setTimeout(packetTimeout);
    return pdu;
  }

  public SnmpVarBind createVarBind(MibOperations mibOps, String columnOid, String value, String targetOid) throws SnmpException {
    MibNode mibNode = mibOps.getMibNode(columnOid);
    SnmpVar var = SnmpVar.createVariable(value, mibNode.getSyntax().getType());
    return new SnmpVarBind(new SnmpOID(targetOid), var);
  }
}
